﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using edowork.com.SQL2005;

namespace WebInvoicingsys.system.settle
{
    public partial class settleclose_detail : System.Web.UI.Page
    {
        //前台输出提示信息变量
        public string alert_msg = "";

        //传入的参数
        string id, years, months;

        /// <summary>
        /// 取得传入的参数
        /// </summary>
        /// <returns></returns>
        private bool GetParma()
        {
            try
            {
                id = Request.Params["id"].ToString();
                years = Request.Params["years"].ToString();
                months = Request.Params["months"].ToString();
            }
            catch
            {
                //显示出错信息
                alert_msg = String.Format(Scripts.alert_warning, Errormsg.er_Parampost);
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);
                return false;
            }

            return true;
        }

        /// <summary>
        /// 查询数据
        /// </summary>
        /// <returns></returns>
        private DataSet GetData()
        {
            //取得传入的参数
            if (GetParma() == false)
            {
                return null;
            }

            Execute exec = new Execute(Session["connectionString"].ToString());

            string sql = "", error_msg = "";
            DataSet ds = null;

            DataSet dsResult = new DataSet();

            sql = "select billtype, qty1 from sys_monthly_bills where sysmid='{0}'";

            sql = String.Format(sql, id);

            ds = exec.ExecSQLQueryDataSet(sql, ref error_msg);

            if (ds == null)
            {
                //显示出错信息
                alert_msg = String.Format(Scripts.alert_warning, error_msg);
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);
                return null;
            }

            DataTable dtBill = new DataTable("BillList");

            dtBill.Columns.Add("dgvID");
            dtBill.Columns.Add("billtype");
            dtBill.Columns.Add("qty1");

            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                DataRow drnew = dtBill.NewRow();

                drnew["dgvID"] = (dtBill.Rows.Count + 1).ToString();
                drnew["billtype"] = dr["billtype"].ToString();
                drnew["qty1"] = dr["qty1"].ToString();

                dtBill.Rows.Add(drnew);
            }

            dsResult.Tables.Add(dtBill);

            sql = @"select b.warehousename, c.areaname, d.storagename, a.productcode, e.productname, a.qty1, a.qty2
                              from sys_monthly_stock a, warehouse b, area c, storage d, product e
                               where a.sysmid='{0}'
                                 and a.warehouseid=b.warehouseid
                                 and a.areaid=c.areaid
                                 and a.storageid=d.storageid
                                 and a.productcode=e.productcode";


            sql = String.Format(sql, id);

            ds = exec.ExecSQLQueryDataSet(sql, ref error_msg);

            if (ds == null)
            {
                //显示出错信息
                alert_msg = String.Format(Scripts.alert_warning, error_msg);
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);
                return null;
            }

            DataTable dtStock = new DataTable("StockList");

            dtStock.Columns.Add("dgvID");
            dtStock.Columns.Add("warehousename");
            dtStock.Columns.Add("areaname");
            dtStock.Columns.Add("storagename");
            dtStock.Columns.Add("productcode");
            dtStock.Columns.Add("productname");
            dtStock.Columns.Add("qty1");
            dtStock.Columns.Add("qty2");

            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                DataRow drnew = dtStock.NewRow();

                drnew["dgvID"] = (dtStock.Rows.Count + 1).ToString();
                drnew["warehousename"] = dr["warehousename"].ToString();
                drnew["areaname"] = dr["areaname"].ToString();
                drnew["storagename"] = dr["storagename"].ToString();
                drnew["productcode"] = dr["productcode"].ToString();
                drnew["productname"] = dr["productname"].ToString();
                drnew["qty1"] = dr["qty1"].ToString();
                drnew["qty2"] = dr["qty2"].ToString();

                dtStock.Rows.Add(drnew);
            }

            dsResult.Tables.Add(dtStock);

            return dsResult;
        }

        /// <summary>
        /// 绑定数据
        /// </summary>
        private void bind()
        {
            DataSet ds = GetData();

            if (ds == null)
            {
                return;
            }

            rpBill.DataSource = ds.Tables[0];
            rpBill.DataBind();

            rplist.DataSource = ds.Tables[1];
            rplist.DataBind();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //查询
                bind();
            }
        }

        //返回
        protected void btnReturn_Click(object sender, EventArgs e)
        {
            string url = "/system/settle/settleclose_list.aspx?years={0}&months={1}";

            url = String.Format(url, years, months);

            Response.Redirect(url);
        }
    }
}